package tuiLayer;

import controlLayer.*;
import modelLayer.*;
import java.util.Scanner;

public class DvdUI {
    private DvdCtrl dvdCtrl;
    private Dvd d;
    
    public DvdUI() {
        dvdCtrl = new DvdCtrl();
    }
    
    /**
     * Dvd menu methods
     */
    public void dvdsMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeDvdsMenu();
            
            switch(choice) {
            case 1 : createDvd();
                break;
            case 2 : findDvd();
                break;
            case 3 : updateDvd();
                break;
            case 4 : removeDvd();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    }
    
    private int writeDvdsMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Dvd Menu ***");
        System.out.println("1. Add dvd ");
        System.out.println("2. Find dvd ");
        System.out.println("3. Update dvd ");
        System.out.println("4. Remove dvd ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    /**
     * Dvd input information
     */
    public int inputBarcode() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the barcode of the dvd (numeric value) : ");
        int barcode = keyboard.nextInt();
        return barcode;
    }
    
    private String inputTitle() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the title : ");
        String title = keyboard.nextLine();
        return title;
    }
    
    private String inputArtist() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the artist : ");
        String artist = keyboard.nextLine();
        return artist;
    }
    
    private String inputPublicationDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the publication date : ");
        String publicationDate = keyboard.nextLine();
        return publicationDate;
    }
    
    /**
     * Dvd show information
     */
    private void showTitle(String title)
    {
        System.out.println("Title : " + title);
    }
    
    private void showArtist(String artist)
    { 
        System.out.println("Artist : " + artist);
    }
    
    private void showPublicationDate(String publicationDate)
    { 
        System.out.println("Publication Date : " + publicationDate);
    }
    
    /**
     * Find dvd method 
     **/
     private void findDvd() {
        int barcode = inputBarcode();
        d = (dvdCtrl.findDvd(barcode));
        if (d != null) {
            System.out.println();
            showTitle(d.getTitle());
            showArtist(d.getArtist());
            showPublicationDate(d.getPublicationDate());
            System.out.println("Number of available copies : " + d.getCopiesNo());
            System.out.println();
            dvdFindMenu();
        }
        else {
            System.out.println("No dvd was found with the specified barcode.");
        }
    }
    
    /**
     * Update dvd
     */
    private void updateDvd() {
        Scanner keyboard = new Scanner(System.in);
        int barcode = inputBarcode();
        d = (dvdCtrl.findDvd(barcode));
        boolean stop = false;
        if (d != null) {
            showTitle(d.getTitle());
            showArtist(d.getArtist());
            showPublicationDate(d.getPublicationDate());
            
            System.out.println();
            System.out.println("1. Update barcode");
            System.out.println("2. Update title");
            System.out.println("3. Update artist");
            System.out.println("4. Update publication date");
            System.out.println("5. Return to previous menu");
            
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {    
                case 1 : d.setBarcode(inputBarcode());
                    break;
                case 2 : d.setTitle(inputTitle());
                    break;
                case 3 : d.setArtist(inputArtist());
                    break;
                case 4 : d.setPublicationDate(inputPublicationDate());
                    break;
                case 5 : stop = true;
                    break;
                }
                System.out.println();
                System.out.println("The dvd information was successfully updated");
            }
        }
        
        else {
            System.out.println("No dvd was found with the specified barcode.");
        }
        System.out.println();
        MainMenuUI.pause();
    }
    
    /**
     * Create dvd method
     */
    private void createDvd() {
        int barcode = inputBarcode();
        String title = inputTitle();
        String artist = inputArtist();
        String pubDate = inputPublicationDate();
        
        dvdCtrl.createDvd(barcode, title, artist, pubDate);
        System.out.println();
        System.out.println("The DVD was successfully created");
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void removeDvd() {
        Scanner keyboard = new Scanner(System.in);
        int barcode = inputBarcode();
        d = (dvdCtrl.findDvd(barcode));
        boolean stop = false;
        if (d != null) {
            System.out.println();
            showTitle(d.getTitle());
            showArtist(d.getArtist());
            showPublicationDate(d.getPublicationDate());
            System.out.println("Number of available copies : " + d.getCopiesNo());
            
            System.out.println();
            System.out.println("1. Remove dvd");
            System.out.println("0. Return to previous menu");
            int choice = keyboard.nextInt();
            
            while (!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : dvdCtrl.removeDvd(d);
                    System.out.println();
                    System.out.println("The DVD was successfully removed");
                    System.out.println();
                    MainMenuUI.pause();
                    break;
                case 0 : stop = true;
                    break;
                }
            }
        }
        else {
            System.out.println("No dvd was found with the specified barcode.");
        }
    }
    
    /**
     * Find dvd submenu
     */
    private void dvdFindMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeDvdFindMenu();
            
            switch(choice) {
            case 1 : createCopy();
                break;
            case 2 : findCopy();
                break;
            case 3 : removeCopy();
                break;
            case 0 : stop = true; 
                break;
            default : System.out.println("Invalid command");
                break;
            }
        }
    }
    
    private int writeDvdFindMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Find DVD Submenu ***");
        System.out.println("1. Add copy ");
        System.out.println("2. Find copy ");
        System.out.println("3. Delete copy ");
        System.out.println("0. Return to previous menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    } 
    
    private void createCopy() {
        int serialNumber = inputSerialNumber();
        String purchaseDate = inputPurchaseDate();
        double purchasePrice = inputPurchasePrice();
        
        dvdCtrl.createCopy(serialNumber, purchaseDate, purchasePrice);
        System.out.println();
        System.out.println("The copy was successfully created");
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void findCopy() {
        int serialNumber = inputSerialNumber();
        Copy c = (dvdCtrl.findCopy(serialNumber));
        if (c != null) {
            System.out.println();
            showPurchaseDate(c.getPurchaseDate());
            showPurchasePrice(c.getPurchasePrice());
        }
        else {
            System.out.println("No copy was found with the specified serial number.");
        }
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void removeCopy() {
        Scanner keyboard = new Scanner(System.in);
        int serialNumber = inputSerialNumber();
        Copy c = (dvdCtrl.findCopy(serialNumber));
        boolean stop = false;
        if (c != null) {
            System.out.println();
            showPurchaseDate(c.getPurchaseDate());
            showPurchasePrice(c.getPurchasePrice());
            
            System.out.println();
            System.out.println("1. Remove copy");
            System.out.println("0. Return to previous menu");
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : dvdCtrl.removeCopy(c);
                        System.out.println();
                        System.out.println("The copy was successfully removed");
                        d.setCopiesNo(d.getCopiesNo()-1);
                        System.out.println();
                        MainMenuUI.pause();
                    break;
                case 0 : stop = true;
                    break;
               }
            }
        }
        else {
            System.out.println("No copy was found with the specified serial number.");
        }
    }
    
    public int inputSerialNumber() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the serial number of the copy (numeric value) : ");
        int serialNumber = keyboard.nextInt();
        return serialNumber;
    }
    
    private String inputPurchaseDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the purchase date : ");
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }
    
    private double inputPurchasePrice() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the purchase price ($): ");
        double purchasePrice = keyboard.nextDouble();
        return purchasePrice;
    }
    
    public void showPurchaseDate(String purchaseDate)
    {
        System.out.println("Purchase date : " + purchaseDate);
    }
    
    public void showPurchasePrice(double purchasePrice)
    { 
        System.out.println("Purchase price : " + purchasePrice);
    }
}
